*** Figure B5

* United States
import excel "$path\Output\Compile_Tables.xlsx", firstrow cellrange(A3:F22) clear sheet(FigB5)
drop in 1
foreach var in employed high_occ high_med_occ logearnedincome Eng_well{
	replace `var' = subinstr(`var',"*","",.)
	replace `var' = subinstr(`var',"(","",.)
	replace `var' = subinstr(`var',")","",.)
}
destring employed-Eng_well, replace

foreach var in employed high_occ high_med_occ logearnedincome Eng_well{
	gen `var'_se = .
	replace `var'_se = `var'[_n+1] if mod(_n,2)==1
	gen `var'_ci_high = `var' + 1.96*`var'_se
	gen `var'_ci_low = `var' - 1.96*`var'_se
}

drop if mod(_n,2) == 0
gen year = substr(VARIABLES,length(VARIABLES)-1,length(VARIABLES))
destring year, replace
replace year = 1900+year if year>30
replace year = 2000+year if year<30
gen group = substr(VARIABLES,4,length(VARIABLES)-7)
drop VARIABLES
order group year
sort group year

twoway (line employed year if group=="btw_75_80", lcolor(gs0) title("Employed" " ") xtitle("Survey Year") ytitle("Difference-in-Difference Coefficient") xlabel(, angle(45)) graphregion(color(white))) (rcap employed_ci_high employed_ci_low year if group=="btw_75_80", lcolor(gs0)) (line employed year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap employed_ci_high employed_ci_low year if group=="btw_87_90", lcolor(gs4)) (line employed year if group=="btw_95_00", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap employed_ci_high employed_ci_low year if group=="btw_95_00", lcolor(gs8) ylabel(, format(%3.2f))) 
graph save "$path/Tempfiles/figb5_US_1.gph", replace

twoway (line high_occ year if group=="btw_75_80", lcolor(gs0) title("High" "Skill Job") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap high_occ_ci_high high_occ_ci_low year if group=="btw_75_80", lcolor(gs0)) (line high_occ year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap high_occ_ci_high high_occ_ci_low year if group=="btw_87_90", lcolor(gs4)) (line high_occ year if group=="btw_95_00", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap high_occ_ci_high high_occ_ci_low year if group=="btw_95_00", lcolor(gs8) ylabel(, format(%3.2f))) 
graph save "$path/Tempfiles/figb5_US_2.gph", replace

twoway (line high_med_occ year if group=="btw_75_80", lcolor(gs0) title("High or Medium" "Skill Job") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap high_med_occ_ci_high high_med_occ_ci_low year if group=="btw_75_80", lcolor(gs0)) (line high_med_occ year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap high_med_occ_ci_high high_med_occ_ci_low year if group=="btw_87_90", lcolor(gs4)) (line high_med_occ year if group=="btw_95_00", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap high_med_occ_ci_high high_med_occ_ci_low year if group=="btw_95_00", lcolor(gs8) ylabel(, format(%3.2f))) 
graph save "$path/Tempfiles/figb5_US_3.gph", replace

twoway (line Eng_well year if group=="btw_75_80", lcolor(gs0) title("Speaks" "English Well") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap Eng_well_ci_high Eng_well_ci_low year if group=="btw_75_80", lcolor(gs0)) (line Eng_well year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap Eng_well_ci_high Eng_well_ci_low year if group=="btw_87_90", lcolor(gs4)) (line Eng_well year if group=="btw_95_00", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap Eng_well_ci_high Eng_well_ci_low year if group=="btw_95_00", lcolor(gs8) ylabel(, format(%3.2f))) 
graph save "$path/Tempfiles/figb5_US_4.gph", replace

twoway (line logearnedincome year if group=="btw_75_80", lcolor(gs0) title("Log Earned" "Income") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap logearnedincome_ci_high logearnedincome_ci_low year if group=="btw_75_80", lcolor(gs0)) (line logearnedincome year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap logearnedincome_ci_high logearnedincome_ci_low year if group=="btw_87_90", lcolor(gs4)) (line logearnedincome year if group=="btw_95_00", lcolor(gs8) lpattern(shortdash) legend(row(1) order(1 "Early Refugees" 3 "Late Refugees" 5 "Economic Migrants"))) (rcap logearnedincome_ci_high logearnedincome_ci_low year if group=="btw_95_00", lcolor(gs8) ylabel(, format(%3.2f))) 
graph save "$path/Tempfiles/figb5_US_5.gph", replace

grc1leg "$path/Tempfiles/figb5_US_1.gph" "$path/Tempfiles/figb5_US_2.gph" "$path/Tempfiles/figb5_US_3.gph" "$path/Tempfiles/figb5_US_4.gph" "$path/Tempfiles/figb5_US_5.gph", rows(1) ycommon graphregion(color(white)) iscale(0.7) leg("$path/Tempfiles/figb5_US_5.gph")
graph export "$path/Output/figb5_US.pdf", replace

sleep 500
forvalues i=1/5{
erase "$path/Tempfiles/figb5_US_`i'.gph"
}

* Germany
import excel "$path\Output\Compile_Tables.xlsx", firstrow cellrange(H3:L24) clear sheet(FigB5)
drop in 1
foreach var in employed high_occ high_med_occ log_income{
	replace `var' = subinstr(`var',"*","",.)
	replace `var' = subinstr(`var',"(","",.)
	replace `var' = subinstr(`var',")","",.)
}
destring employed-log_income, replace

foreach var in employed high_occ high_med_occ log_income{
	gen `var'_se = .
	replace `var'_se = `var'[_n+1] if mod(_n,2)==1
	gen `var'_ci_high = `var' + 1.96*`var'_se
	gen `var'_ci_low = `var' - 1.96*`var'_se
}

drop if mod(_n,2) == 0
gen year = substr(VARIABLES,length(VARIABLES)-1,length(VARIABLES))
destring year, replace
replace year = 1900+year if year>30
replace year = 2000+year if year<30
gen group = substr(VARIABLES,4,length(VARIABLES)-7)
drop VARIABLES
order group year
sort group year

twoway (line employed year if group=="btw_80_84", lcolor(gs0) title("Employed" " ") xtitle("Survey Year") ytitle("Difference-in-Difference Coefficient") xlabel(, angle(45)) graphregion(color(white))) (rcap employed_ci_high employed_ci_low year if group=="btw_80_84", lcolor(gs0)) (line employed year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap employed_ci_high employed_ci_low year if group=="btw_87_90", lcolor(gs4)) (line employed year if group=="btw_95_99", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap employed_ci_high employed_ci_low year if group=="btw_95_99", lcolor(gs8) ylabel(, format(%2.1f))) 
graph save "$path/Tempfiles/figb5_Germany_1.gph", replace

twoway (line high_occ year if group=="btw_80_84", lcolor(gs0) title("High" "Skill Job") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap high_occ_ci_high high_occ_ci_low year if group=="btw_80_84", lcolor(gs0)) (line high_occ year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap high_occ_ci_high high_occ_ci_low year if group=="btw_87_90", lcolor(gs4)) (line high_occ year if group=="btw_95_99", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap high_occ_ci_high high_occ_ci_low year if group=="btw_95_99", lcolor(gs8) ylabel(, format(%2.1f))) 
graph save "$path/Tempfiles/figb5_Germany_2.gph", replace

twoway (line high_med_occ year if group=="btw_80_84", lcolor(gs0) title("High or Medium" "Skill Job") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap high_med_occ_ci_high high_med_occ_ci_low year if group=="btw_80_84", lcolor(gs0)) (line high_med_occ year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap high_med_occ_ci_high high_med_occ_ci_low year if group=="btw_87_90", lcolor(gs4)) (line high_med_occ year if group=="btw_95_99", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap high_med_occ_ci_high high_med_occ_ci_low year if group=="btw_95_99", lcolor(gs8) ylabel(, format(%2.1f))) 
graph save "$path/Tempfiles/figb5_Germany_3.gph", replace

twoway (line log_income year if group=="btw_80_84", lcolor(gs0) title("Log Earned" "Income") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap log_income_ci_high log_income_ci_low year if group=="btw_80_84", lcolor(gs0)) (line log_income year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap log_income_ci_high log_income_ci_low year if group=="btw_87_90", lcolor(gs4)) (line log_income year if group=="btw_95_99", lcolor(gs8) lpattern(shortdash) legend(row(1) order(1 "Early Refugees" 3 "Late Refugees" 5 "Economic Migrants"))) (rcap log_income_ci_high log_income_ci_low year if group=="btw_95_99", lcolor(gs8) ylabel(, format(%2.1f))) 
graph save "$path/Tempfiles/figb5_Germany_4.gph", replace

grc1leg "$path/Tempfiles/figb5_Germany_1.gph" "$path/Tempfiles/figb5_Germany_2.gph" "$path/Tempfiles/figb5_Germany_3.gph" "$path/Tempfiles/figb5_Germany_4.gph", rows(1) ycommon graphregion(color(white)) iscale(0.7) leg("$path/Tempfiles/figb5_Germany_4.gph")
graph export "$path/Output/figb5_Germany.pdf", replace

sleep 500
forvalues i=1/4{
erase "$path/Tempfiles/figb5_Germany_`i'.gph"
}


*** Figure D2
* United States
import excel "$path\Output\Compile_Tables.xlsx", firstrow cellrange(A3:E22) clear sheet(FigD2)
drop in 1
foreach var in age_alt female yearofimmigration edu_years {
	replace `var' = subinstr(`var',"*","",.)
	replace `var' = subinstr(`var',"(","",.)
	replace `var' = subinstr(`var',")","",.)
}
destring age_alt-edu_years, replace

foreach var in age_alt female yearofimmigration edu_years{
	gen `var'_se = .
	replace `var'_se = `var'[_n+1] if mod(_n,2)==1
	gen `var'_ci_high = `var' + 1.96*`var'_se
	gen `var'_ci_low = `var' - 1.96*`var'_se
}

drop if mod(_n,2) == 0
gen year = substr(VARIABLES,length(VARIABLES)-1,length(VARIABLES))
destring year, replace
replace year = 1900+year if year>30
replace year = 2000+year if year<30
gen group = substr(VARIABLES,4,length(VARIABLES)-7)
drop VARIABLES
order group year
sort group year

twoway (line age_alt year if group=="btw_75_80", lcolor(gs0) title("Age" " ") xtitle("Survey Year") ytitle("Difference-in-Difference Coefficient") xlabel(, angle(45)) graphregion(color(white))) (rcap age_alt_ci_high age_alt_ci_low year if group=="btw_75_80", lcolor(gs0)) (line age_alt year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap age_alt_ci_high age_alt_ci_low year if group=="btw_87_90", lcolor(gs4)) (line age_alt year if group=="btw_95_00", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap age_alt_ci_high age_alt_ci_low year if group=="btw_95_00", lcolor(gs8) yscale(range(-0.2 0.2)) ylabel(-0.2(0.1)0.2, format(%3.1f))) 
graph save "$path/Tempfiles/figd2_US_1.gph", replace

twoway (line female year if group=="btw_75_80", lcolor(gs0) title("Female" "") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap female_ci_high female_ci_low year if group=="btw_75_80", lcolor(gs0)) (line female year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap female_ci_high female_ci_low year if group=="btw_87_90", lcolor(gs4)) (line female year if group=="btw_95_00", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap female_ci_high female_ci_low year if group=="btw_95_00", lcolor(gs8)yscale(range(-0.2 0.2)) ylabel(-0.2(0.1)0.2, format(%3.1f))) 
graph save "$path/Tempfiles/figd2_US_2.gph", replace

twoway (line yearofimmigration year if group=="btw_75_80", lcolor(gs0) title("Year of" "Immigration") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap yearofimmigration_ci_high yearofimmigration_ci_low year if group=="btw_75_80", lcolor(gs0)) (line yearofimmigration year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap yearofimmigration_ci_high yearofimmigration_ci_low year if group=="btw_87_90", lcolor(gs4)) (line yearofimmigration year if group=="btw_95_00", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap yearofimmigration_ci_high yearofimmigration_ci_low year if group=="btw_95_00", lcolor(gs8) yscale(range(-3 3)) ylabel(-3(1)3, format(%3.0f))) 
graph save "$path/Tempfiles/figd2_US_3.gph", replace

twoway (line edu_years year if group=="btw_75_80", lcolor(gs0) title("Years of" "Education") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap edu_years_ci_high edu_years_ci_low year if group=="btw_75_80", lcolor(gs0)) (line edu_years year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap edu_years_ci_high edu_years_ci_low year if group=="btw_87_90", lcolor(gs4)) (line edu_years year if group=="btw_95_00", lcolor(gs8) lpattern(shortdash) legend(row(1) order(1 "Early Refugees" 3 "Late Refugees" 5 "Economic Migrants"))) (rcap edu_years_ci_high edu_years_ci_low year if group=="btw_95_00", lcolor(gs8) yscale(range(-2 2)) ylabel(-2(1)2, format(%3.0f))) 
graph save "$path/Tempfiles/figd2_US_4.gph", replace

grc1leg "$path/Tempfiles/figd2_US_1.gph" "$path/Tempfiles/figd2_US_2.gph" "$path/Tempfiles/figd2_US_3.gph" "$path/Tempfiles/figd2_US_4.gph", rows(1) graphregion(color(white)) iscale(0.7) leg("$path/Tempfiles/figd2_US_4.gph")
graph export "$path/Output/figd2_US.pdf", replace

sleep 500
forvalues i=1/4{
erase "$path/Tempfiles/figd2_US_`i'.gph"
}


*** Germany
import excel "$path\Output\Compile_Tables.xlsx", firstrow cellrange(G3:K24) clear sheet(FigD2)
drop in 1
foreach var in age_alt female immigration_year edu_years {
	replace `var' = subinstr(`var',"*","",.)
	replace `var' = subinstr(`var',"(","",.)
	replace `var' = subinstr(`var',")","",.)
}
destring age_alt-edu_years, replace

foreach var in age_alt female immigration_year edu_years{
	gen `var'_se = .
	replace `var'_se = `var'[_n+1] if mod(_n,2)==1
	gen `var'_ci_high = `var' + 1.96*`var'_se
	gen `var'_ci_low = `var' - 1.96*`var'_se
}

drop if mod(_n,2) == 0
gen year = substr(VARIABLES,length(VARIABLES)-1,length(VARIABLES))
destring year, replace
replace year = 1900+year if year>30
replace year = 2000+year if year<30
gen group = substr(VARIABLES,4,length(VARIABLES)-7)
drop VARIABLES
order group year
sort group year

twoway (line age_alt year if group=="btw_80_84", lcolor(gs0) title("Age" " ") xtitle("Survey Year") ytitle("Difference-in-Difference Coefficient") xlabel(, angle(45)) graphregion(color(white))) (rcap age_alt_ci_high age_alt_ci_low year if group=="btw_80_84", lcolor(gs0)) (line age_alt year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap age_alt_ci_high age_alt_ci_low year if group=="btw_87_90", lcolor(gs4)) (line age_alt year if group=="btw_95_99", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap age_alt_ci_high age_alt_ci_low year if group=="btw_95_99", lcolor(gs8) yscale(range(-0.6 0.6)) ylabel(-0.6(0.2)0.6, format(%3.1f))) 
graph save "$path/Tempfiles/figd2_Germany_1.gph", replace

twoway (line female year if group=="btw_80_84", lcolor(gs0) title("Female" "") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap female_ci_high female_ci_low year if group=="btw_80_84", lcolor(gs0)) (line female year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap female_ci_high female_ci_low year if group=="btw_87_90", lcolor(gs4)) (line female year if group=="btw_95_99", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap female_ci_high female_ci_low year if group=="btw_95_99", lcolor(gs8)yscale(range(-0.3 0.3)) ylabel(-0.3(0.2)0.3, format(%3.1f))) 
graph save "$path/Tempfiles/figd2_Germany_2.gph", replace

twoway (line immigration_year year if group=="btw_80_84", lcolor(gs0) title("Year of" "Immigration") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap immigration_year_ci_high immigration_year_ci_low year if group=="btw_80_84", lcolor(gs0)) (line immigration_year year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap immigration_year_ci_high immigration_year_ci_low year if group=="btw_87_90", lcolor(gs4)) (line immigration_year year if group=="btw_95_99", lcolor(gs8) lpattern(shortdash) legend(off)) (rcap immigration_year_ci_high immigration_year_ci_low year if group=="btw_95_99", lcolor(gs8) yscale(range(-8 -4)) ylabel(-8(1)-4, format(%3.0f))) 
graph save "$path/Tempfiles/figd2_Germany_3.gph", replace

twoway (line edu_years year if group=="btw_80_84", lcolor(gs0) title("Years of" "Education") xtitle("Survey Year") ytitle("") xlabel(, angle(45)) graphregion(color(white))) (rcap edu_years_ci_high edu_years_ci_low year if group=="btw_80_84", lcolor(gs0)) (line edu_years year if group=="btw_87_90", lcolor(gs4) lpattern(dash)) (rcap edu_years_ci_high edu_years_ci_low year if group=="btw_87_90", lcolor(gs4)) (line edu_years year if group=="btw_95_99", lcolor(gs8) lpattern(shortdash) legend(row(1) order(1 "Early Refugees" 3 "Late Refugees" 5 "Economic Migrants"))) (rcap edu_years_ci_high edu_years_ci_low year if group=="btw_95_99", lcolor(gs8) yscale(range(-3 3)) ylabel(-3(1)3, format(%3.0f))) 
graph save "$path/Tempfiles/figd2_Germany_4.gph", replace

grc1leg "$path/Tempfiles/figd2_Germany_1.gph" "$path/Tempfiles/figd2_Germany_2.gph" "$path/Tempfiles/figd2_Germany_3.gph" "$path/Tempfiles/figd2_Germany_4.gph", rows(1) graphregion(color(white)) iscale(0.7) leg("$path/Tempfiles/figd2_Germany_4.gph")
graph export "$path/Output/figd2_Germany.pdf", replace

sleep 500
forvalues i=1/4{
erase "$path/Tempfiles/figd2_Germany_`i'.gph"
}